#pragma once
#ifndef WIN32
#include <sys/socket.h>    
#include <sys/epoll.h>    
#include <netinet/in.h>    
#include <arpa/inet.h>    
#include <fcntl.h>    
#include <unistd.h>    
#include <stdio.h>    
#include <errno.h>    
#include <iostream>  

#include "BaseRebot.h"
using namespace std;    
#define MAX_EVENTS 50000 

struct MyEvent_s    
{    
    int fd;    
    void (*call_back)(int fd, int events, void *arg);    
    int events;    
    void *arg;    
    int status; // 1: in epoll wait list, 0 not in    
    char buff[128]; // recv data buffer    
    int len, s_offset;    
    long last_active; // last active time    
};


class CEpollNet: public CThread
{
public:
	CEpollNet(void);
	~CEpollNet(void);

	void EventSet(MyEvent_s *ev, int fd, void (*call_back)(int, int, void*), void *arg);
	void EventAdd(int epollFd, int events, MyEvent_s *ev);
	void EventDel(int epollFd, MyEvent_s *ev);

	void AcceptConn(int fd, int events, void *arg);
	void RecvData(int fd, int events, void *arg);
	void SendData(int fd, int events, void *arg);

	void InitListenSocket(int epollFd, short port);
	void Init(const char * host, unsigned int port, string& serverType);
	void setnonblocking(int sockfd);

	virtual unsigned ThreadProc();
public:
	int g_epollFd;    
	MyEvent_s g_Events[MAX_EVENTS+1]; 

};

#endif

